package com.ty.paypal.controller;

import java.io.IOException;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paypal.http.HttpResponse;
import com.paypal.orders.Order;
import com.ty.paypal.VO.ResultVO;
import com.ty.paypal.core.Plan;
import com.ty.paypal.core.Product;
import com.ty.paypal.core.Subscriptions;
import com.ty.paypal.service.order.GetOrder;
import com.ty.paypal.service.order.RefundOrder;
import com.ty.paypal.service.order.authorize.AuthorizeOrder;
import com.ty.paypal.service.order.authorize.CaptureOrder;
import com.ty.paypal.service.order.capture.CreateOrder;
import com.ty.paypal.service.plan.CreatePlans;
import com.ty.paypal.service.plan.CreateProduct;
import com.ty.paypal.service.plan.UseSubscriptions;
import com.ty.paypal.utils.ResultVOUtil;

/**
* @author ty
**/

@Controller
public class IndexController {

	/**
	 * 创建支付
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "create-paypal-transaction")
    public ResultVO<String> create() {
		try {
			Order order = new CreateOrder().createOrder(true).result();
			return ResultVOUtil.success(order.id());
		} catch (IOException e) {
			e.printStackTrace();
		}
	    return null;
    }
    
	/**
	 * 创建授权支付
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "create-paypal-transaction2")
	public ResultVO<String> create2() {
		try {
			Order order = new  com.ty.paypal.service.order.authorize.CreateOrder().createOrder(true).result();
			return ResultVOUtil.success(order.id());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 授权
	 * @param map
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "paypal-authorize")
    public ResultVO authorize(String orderId) {
		try { 
			new AuthorizeOrder().authorizeOrder(orderId, true);} 
		catch (IOException e) {
			e.printStackTrace(); 
		}
		
    	return ResultVOUtil.success();
    }
    
    /**
     * 收取授权的钱
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "paypal-capture")
    public ResultVO capture(String authorizationId) {
    	try { 
    		new CaptureOrder().captureOrder(authorizationId, true);
    	} 
    	catch (IOException e) {
    		e.printStackTrace(); 
    	}
    	
    	return ResultVOUtil.success();
    }
    
    /**
     * 验证支付
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "paypal-transaction-complete")
    public ResultVO check( @RequestBody Map<String, Object> map) {
    	String orderID = map.get("orderID").toString();
    	try { 
    		new GetOrder().getOrder(orderID); } 
    	catch (IOException e) {
    		e.printStackTrace(); 
    	}
    	
    	return ResultVOUtil.success();
    }
    
    /**
     * 退款
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "paypal-refund")
    public ResultVO refund( String captureId) {
//    	String captureId = map.get("captureId").toString();
		try {
		      new RefundOrder().refundOrder(captureId, true);
		    } catch (Exception e) {
		      e.printStackTrace();
		}
    	return ResultVOUtil.success();
    }
    
    /**
     * 创建一个计划
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "paypal-planId")
    public ResultVO getPlanId() {
    	try { 
    		HttpResponse<Product> responseProduct = new CreateProduct().createProduct(true);
    		String productId = responseProduct.result().getId();
    		HttpResponse<Plan> responsePlan = new CreatePlans().billingPlans(productId);
    		if(responsePlan!=null) {
    			return ResultVOUtil.success(responsePlan.result().getId());
    		}
    	} 
    	catch (IOException e) {
    		e.printStackTrace(); 
    	}
    	
    	return ResultVOUtil.success();
    }
    
    /**
     * 使用订阅
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "paypal-useSub")
    public ResultVO useSub(String planId) {
    	try { 
    		HttpResponse<Subscriptions> response = new UseSubscriptions().use(planId);
    	} 
    	catch (IOException e) {
    		e.printStackTrace(); 
    	}
    	
    	return ResultVOUtil.success();
    }
}
